/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package provider.impl.genomemap.me;

import commons.core.ModelEstimator;
import genomemap.data.TAB08Data;
import genomemap.data.TAB08Data.Genotype;
import genomemap.model.TAB08;
import org.apache.commons.lang.ArrayUtils;

import java.util.Arrays;

/**
 * @author Susanta Tewari
 */
public class TAB08IntervalMLE
        extends ModelEstimator<TAB08, TAB08Data, TAB08Data, TAB08IntervalMLE> {

    /**
     * Constructs ...
     *
     *
     * @param d
     */
    public TAB08IntervalMLE(final TAB08Data d) {
        super(d);
    }

    /**
     * Method description
     *
     *
     * @param data
     *
     * @return
     */
    @Override
    public TAB08IntervalMLE factory(final TAB08Data data) {
        return new TAB08IntervalMLE(data);
    }

    /**
     * Method description
     *
     *
     * @return
     */
    @Override
    public TAB08 getEstimate() {


        // get data
        final byte[][] f = new byte[getData().genotypeCount()][];
        final int[] n    = new int[getData().genotypeCount()];
        int counter      = 0;

        for (final Genotype genotype : getData()) {

            f[counter] = genotype.getScore();
            n[counter] = genotype.getFreq();

            counter++;
        }

        final int N               = getData().getSampleSize();
        final double[] c_estimate = new double[getData().getModel().getLoci() - 1];

        for (int i = 0; i < c_estimate.length; i++) {


            // count the number of recombinants
            int count = 0;

            for (int j = 0; j < f.length; j++) {

                if (f[j][i] != f[j][i + 1]) {
                    count += n[j];
                }
            }


            // estimate c
            double c       = 0;
            final double d = (double) 2 * count / N;

            if (d > 1.0) {
                c = 1.0 - (double) 2 / N;    // correction applied to avoid boundary value
            } else {
                c = 1 - Math.sqrt(1 - d);
            }


            // correction applied to avoid boundary value
            if ((c == 0) || (c > 1.0) || (c < 0)) {
                c = 0.0001;
            }

            c_estimate[i] = c;
        }

        return getData().getModel().create(Arrays.asList(ArrayUtils.toObject(c_estimate)));
    }
}
